/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source 
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.  
 */

package jdbcmanager.tree;

import java.util.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import jdbcmanager.app.*;
import jdbcmanager.swing.table.*;
import jdbcmanager.dbtools.*;
import jdbcmanager.dbtools.agentwise.*;
import jdbcmanager.dbtools.datatypes.*;

/**
 * Creates a modal dialog where properties of current DBMS are shown structured
 * in areas (tabs are used to acomplish this).
 *  
 * @author Francisco Morero Peyrona
 */
final class Info4DBMS extends JTabbedPane
{
   private AgentMetaData dbmd = DbUtil.getAgentDBMS().getAgentMetaData();
   
   //-------------------------------------------------------------------------//
   
   Info4DBMS()
   {
      add( getDBInfo()     , "DataBase Info" );
      add( getDriverInfo() , "Driver Info" );
      add( getCatalogs()   , DbUtil.getAgentDBMS().getAgentMetaData().getCatalogTerm() +'s' );
      add( getSchemas()    , DbUtil.getAgentDBMS().getAgentMetaData().getSchemaTerm()  +'s' );
      add( getTableTypes() , "Table Types" );
      add( getDataTypes()  , "Data Types" );
      add( getKeywords()   , "Keywords" );
      add( getBultInFuncs(), "Built-in functions" );
   }
   
   private JPanel getDBInfo()
   {
      JPanel     panel = new JPanel( new BorderLayout() );
      JxTableMap table = new JxTableMap();
                 table.setAutoResizeMode( JTable.AUTO_RESIZE_ALL_COLUMNS );
                 table.setAutoResizeMode( JTable.AUTO_RESIZE_ALL_COLUMNS );
                 table.addRow( "Product Name"   , this.dbmd.getDatabaseProductName() );
                 table.addRow( "Product Version", this.dbmd.getDatabaseProductVersion() );
                 table.addRow( "Major Version"  , this.dbmd.getDatabaseMajorVersion() ); 
                 table.addRow( "Minor Version"  , this.dbmd.getDatabaseMinorVersion() );
        
        panel.add( new JScrollPane( table ), BorderLayout.CENTER );

      return panel;
   }

   private JPanel getDriverInfo()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      
      try
      {
         JxTableMap table = new JxTableMap(); 
                    table.setAutoResizeMode( JTable.AUTO_RESIZE_ALL_COLUMNS );
                    table.addRow( "Driver Name"         , this.dbmd.getDriverName() );
                    table.addRow( "Driver Version"      , this.dbmd.getDriverVersion() );
                    table.addRow( "Driver Major Version", this.dbmd.getDriverMajorVersion() );
                    table.addRow( "Driver Minor Version", this.dbmd.getDriverMinorVersion() );
                    table.addRow( "JDBC Major Version"  , this.dbmd.getJDBCMajorVersion() ); 
                    table.addRow( "JDBC Minor Version"  , this.dbmd.getJDBCMinorVersion() );
        
         panel.add( new JScrollPane( table ), BorderLayout.CENTER );
      }
      catch( Exception exc )
      {
         App.showDebugException( exc );
         panel.add( new JLabel( "Error retrieving this information", SwingConstants.CENTER ), BorderLayout.CENTER );
      }

      return panel;
   }
  
   private JPanel getCatalogs()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      JList  list  = new JList( DbUtil.getAgentDBMS().getAgentMetaData().getCatalogs() );
      
      panel.add( new JScrollPane( list ), BorderLayout.CENTER );
      
      return panel;
   }   
   
   private JPanel getSchemas()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      JList  list  = new JList( DbUtil.getAgentDBMS().getAgentMetaData().getSchemas() );
      
      panel.add( new JScrollPane( list ), BorderLayout.CENTER );
      
      return panel;
   }
   
   private JPanel getTableTypes()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      JList  list  = new JList( DbUtil.getAgentDBMS().getAgentMetaData().getTableTypes() );
      
      panel.add( new JScrollPane( list ), BorderLayout.CENTER );
      
      return panel;
   }
         
   private JPanel getDataTypes()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      
      try
      {
         JxTableRamRowSet table = new JxTableRamRowSet( DbUtil.getAgentDBMS().getAgentMetaData().getTypeInfo() );
         
         // Changes SQL type number by its name
         Vector vData = table.getData();
         
         for( int n = 0; n < vData.size(); n++ )
         {
            Vector vRow  = (Vector) vData.get( n );
            Object oType = vRow.get( 1 );
            
            // Some drivers use an Integer and other a Short
            if( oType.getClass().getName().endsWith( "Short" ) )
               oType = JDBCTypeConverter.getJdbcTypeName( ((Short) oType).intValue() );
            else if( oType.getClass().getName().endsWith( "Integer" ) )
               oType = JDBCTypeConverter.getJdbcTypeName( ((Integer) oType).intValue() );
            
            // If it is not a short, neither an integer, just leave it like it is
            
            vRow.set( 1, oType );
         }
         
         panel.add( new JScrollPane( table ), BorderLayout.CENTER );
      }
      catch( Exception exc )
      {
         App.showDebugException( exc );
         panel.add( new JLabel( "Error retrieving this information", SwingConstants.CENTER ), BorderLayout.CENTER );
      }
      
      return panel;
   }
      
   private JPanel getKeywords()
   {
      JPanel panel = new JPanel( new BorderLayout() );
      JList  list  = new JList( DbUtil.getAgentDBMS().getAgentMetaData().getExtraSQLKeywords() );
      
      panel.add( new JScrollPane( list ), BorderLayout.CENTER );
      
      return panel;
   }
   
   private JPanel getBultInFuncs()
   {
      JPanel panel = new JPanel( new BorderLayout( 0, 9 ) );
      
      try
      {
         final Hashtable   htFuncs = DbUtil.getAgentDBMS().getAgentMetaData().getPrebuiltFunctions();
         final JComboBox   cmbType = new JComboBox( htFuncs.keySet().toArray() );
         final JList       lstFunc = new JList();
         final JScrollPane spFunc  = new JScrollPane( lstFunc );
         
         cmbType.addActionListener( new ActionListener()
         {
            public void actionPerformed( ActionEvent evt )
            {
               String   sKey = (String) cmbType.getSelectedItem();
               String[] data = (String[]) htFuncs.get( sKey );
               lstFunc.setListData( data );
               
               if( data.length > 0 )
                  lstFunc.setSelectedIndex( 0 );
            } 
         } );
         
         if( cmbType.getItemCount() > 0 )
            cmbType.setSelectedIndex( 0 );
         
         JPanel pnlCombo = new JPanel( new FlowLayout( FlowLayout.LEADING ) );
                pnlCombo.add( new JLabel( "Function Type  " ) );
                pnlCombo.add( cmbType );
         
         panel.setBorder( BorderFactory.createEmptyBorder( 9,9,9,9 ) );
         panel.add( pnlCombo, BorderLayout.NORTH  );
         panel.add( spFunc  , BorderLayout.CENTER );
      }
      catch( Exception exc )
      {
         App.showDebugException( exc );
         panel.add( new JLabel( "Error retrieving this information", SwingConstants.CENTER ), BorderLayout.CENTER );
      }

      return panel;
   }
}